import logging
from dataclasses import dataclass

from src.application.order.dto.order import OrderDTO
from src.application.order.dto.orders import OrdersDTO
from src.application.order.enums import OrderStatus, OrderType
from src.application.order.interfaces import GetOrdersFilters
from src.application.order.uow import OrderUoW
from src.domain.common.constants import Empty, OrderBy

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class GetOrders:
    tg_user_id: int | Empty = Empty.UNSET
    offset: int | Empty = Empty.UNSET
    limit: int | Empty = Empty.UNSET
    order_type: OrderType = OrderType.proxy
    order_by: OrderBy = OrderBy.ASC
    order_status: OrderStatus = OrderStatus.active


class GetOrdersHandler:
    def __init__(self, *, uow: OrderUoW) -> None:
        self._uow = uow

    async def __call__(self, query: GetOrders) -> OrdersDTO:
        filters = GetOrdersFilters(
            tg_user_id=query.tg_user_id,
            offset=query.offset,
            limit=query.limit,
            order_type=query.order_type,
            order_by=query.order_by,
            order_status=query.order_status,
        )
        orders = await self._uow.order_reader.get_orders(filters)
        logger.debug(
            "Get users",
            extra={
                "orders": orders,
                "total": len(orders),
                "offset": query.offset,
                "limit": query.limit,
                "tg_user_id": query.tg_user_id,
            },
        )

        return OrdersDTO(
            orders=orders,
            total=len(orders),
            offset=query.offset,
            limit=query.limit,
            tg_user_id=query.tg_user_id,
        )
